import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_widget/src/config/color_define.dart';
import 'package:flutter_widget/src/widget/image/web_cache_Image.dart';
import 'package:flutter_widget/src/widget/tap_widget.dart';

/*圆形头像*/
class AvatarWidget extends StatelessWidget {
  final String? avatar; //头像地址url
  final String? avatarFrame; //头像框地址
  final GestureTapCallback? onTap; //点击事件
  final double width; //宽
  final double height; //高
  final WebCacheImageScale ratio; //设置图片压缩大小
  final double? borderWidth; //头像边框
  final Color? borderColor; //边框颜色

  final String name; //昵称，如果没有头像会显示昵称首个文字，(根据需求使用)
  final double fontSize; //昵称字号，设置文字才生效
  final Color? backgroundColor; //昵称头像背景色
  final File? file;

  const AvatarWidget({
    Key? key,
    this.avatar = "",
    this.avatarFrame,
    this.onTap,
    this.name = "",
    this.width = 32,
    this.height = 32,
    this.fontSize = 18,
    this.backgroundColor,
    this.ratio = WebCacheImageScale.small,
    this.borderWidth,
    this.borderColor,
    this.file,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String avatarUrl = avatar ?? "";

    return TapWidget(
      onTap: onTap,
      child: SizedBox(
        width: width,
        height: height,
        child: Stack(
          alignment: Alignment.center,
          clipBehavior: Clip.none,
          children: [
            borderColor != null && borderWidth != null
                ? Container(
                    width: width,
                    height: height,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      border: Border.all(
                        color: borderColor ?? FWColorPicker.white,
                        width: borderWidth ?? 1.0,
                      ),
                      borderRadius: BorderRadius.circular(width / 2),
                    ),
                    child: SizedBox(
                      width: width,
                      height: height,
                      child: _circleAvatar(avatarUrl),
                    ),
                  )
                : SizedBox(
                    width: width,
                    height: height,
                    child: _circleAvatar(avatarUrl),
                  ),
            Visibility(
              visible: avatarFrame?.isNotEmpty == true,
              child: Positioned(
                width: width * 1.267,
                height: height * 1.267,
                child: WebCacheImage(
                  imageUrl: avatarFrame,
                  fit: BoxFit.contain,
                  displayPlaceholder: false,
                  scale: WebCacheImageScale.small,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _circleAvatar(String? avatarUrl) {
    ImageProvider? avatar = (avatarUrl?.isNotEmpty == true)
        ? WebImageProvider(
            avatarUrl,
            scale: ratio,
          )
        : const AssetImage(
            "assets/default_avatar.png",
            package: "flutter_widget",
          );

    return CircleAvatar(
      backgroundColor: backgroundColor ?? FWColorPicker.colorFF999999,
      backgroundImage: (file != null) ? FileImage(file!) : avatar,
      child: (avatarUrl != null && avatarUrl.isNotEmpty == true)
          ? null
          : Text(
              name.isNotEmpty ? name[0] : "",
              style: TextStyle(
                color: FWColorPicker.white,
                fontSize: fontSize,
              ),
            ),
    );
  }
}
